#ifndef HANDLEJSON_HPP
#define HANDLEJSON_HPP

#include <stdio.h>
#include <cassert>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include "json11/json11.hpp"
#include <list>
#include <set>
#include <unordered_map>
#include <algorithm>
#include <type_traits>
#include <fstream>
#include "sys/io.h"
#include <unistd.h>
#include"base64/base64.h"

using namespace std;
using namespace json11;

Json ReadJson(string json_path){
    Json error=Json::object{};
    //从文件中读取，保证当前文件有map_json_path文件  
    ifstream in(json_path, ios::binary);

    if (!in.is_open())
    {
		
        cout << "Error opening file"<<json_path<<endl;
		return error;
    }
    stringstream buffer;
    string err;
    buffer << in.rdbuf();
    in.close();
    string value = buffer.str();
    // cout<<value<<endl;

    Json json =Json::parse(value,err);
    
    return json;
}

Json deal_path(string path){
    Json err;
    if( access(path.c_str(),F_OK)!=0){
        	cout << "The file you expected does not exist!!!" << endl;
        	return err;
    }
    ifstream in(path, ios::binary);
    if (!in.is_open()){
        cout << "Error opening file\n";
        return err;
    }
    int i_line=0;
    string s;
    while(getline(in, s)){
        i_line++;
    }
    in.close();

    double arr[i_line][3];
    FILE *fp;
    if((fp=fopen(path.c_str(),"r"))== NULL){
        printf("cannot open this file\n");
        return err;
    }
    for(int j=0;j<i_line;j++){
        for(int i=0;i<3;i++){
            fscanf(fp,"%lf",&arr[j][i]);
        }
    }  
    fclose(fp);

    map<string,float> pathPoint = {{"x",0},
                                {"y",0},
                                {"angle",0}};
    
    Json::array path_json;
    for(int j=0;j<i_line;j++){
        for(int i=0;i<3;i++){
            // printf("%lf ",arr[j][i]);
            if(i==0)pathPoint["x"]=arr[j][i];
            if(i==1)pathPoint["y"]=arr[j][i];
            if(i==2)pathPoint["angle"]=arr[j][i];
        }
        Json map_list_(pathPoint);
        path_json.push_back(map_list_);
        // printf("\n");
    }
    return path_json;
}

Json base64_pgm(string pgmPath){
    Json err=Json::object{};
    if( access(pgmPath.c_str(),F_OK)!=0){
        cout << "The file you expected does not exist!!!" <<pgmPath<< endl;
        return err;
    }
    fstream f;

    f.open(pgmPath.c_str(), ios::in|ios::binary);
    if(!f.is_open()){
        ROS_INFO("open file error!");
        return err;
    }
    f.seekg(0, std::ios_base::end);
    std::streampos sp = f.tellg();
    int size = sp;
    char* buffer = (char*)malloc(sizeof(char)*size);
    f.seekg(0,ios_base::beg);
    f.read(buffer,size);

    cout << "file size:" << size << endl;     
    string imgBase64 = base64_encode(buffer, size);
    cout << "img base64 encode size:" << imgBase64.size() << endl;      
    // string imgdecode64 = base64_decode(imgBase64);
    // cout << "img decode size:" << imgdecode64.size() << endl;
    
    Json data = Json::object{
        {"data",imgBase64}
    };
    return  data;
}

#endif
